package xyz.labmem.controller

import com.dustinredmond.fxtrayicon.FXTrayIcon
import javafx.application.Platform
import javafx.geometry.Pos
import javafx.scene.image.Image
import javafx.scene.image.ImageView
import javafx.stage.Stage
import javafx.util.Duration
import xyz.labmem.core.base.BaseController
import xyz.labmem.core.context.FXApplicationContext
import xyz.labmem.core.reflex.FXApplication
import xyz.labmem.core.util.getResourceFile
import xyz.labmem.core.util.gui.alert.Alert
import xyz.labmem.core.util.gui.alert.AlertUtil
import xyz.labmem.core.util.gui.tray.SystemTrayInterFace

/**
 * 托盘
 * @Author lty
 * @Date 2021/10/14 10:07
 */
class FxTray : SystemTrayInterFace {

    private lateinit var alert: AlertUtil

    var trayIcon: FXTrayIcon? = null

    /**
     * 更改系统托盘所监听的Stage
     */
    override fun listen(controller: BaseController) {
        alert = controller.alertUtil
        if (FXTrayIcon.isSupported()) {
            val stage = controller.stage
            trayIcon = FXTrayIcon.Builder(stage, getResourceFile("ticon.png").url).apply {
                onAction {
                    showStage(stage)
                }
                menuItem("关于") {
                    showStage(stage)
                    Alert(stage).apply {
                        message = "HTML浏览器\"爬虫\" \nversion 0.1 \nBy 【LabFX™】"
                        title = "关于"
                        create()
                    }
                }
                menuItem("退出") {
                    FXApplication.close()
                }
                show()
            }.build()
        } else {
            //系统托盘不支持
            alert.noticeFailed("当前系统不支持托盘！")
        }

    }

    /**
     * 点击系统托盘,显示界面(并且显示在最前面,将最小化的状态设为false)
     */
    private fun showStage(stage: Stage) {
        //点击系统托盘,
        if (stage.isIconified) {
            stage.isIconified = false
        }
        if (stage.isShowing) {
            stage.show()
        } else
            stage.hide()
        stage.toFront()
    }

    fun winMsg(caption: String?, content: String) {
        if (trayIcon != null && FXApplicationContext.sysVersion!! == "Windows 10") {
            trayIcon!!.showMessage(caption ?: "LabApp™ 消息！", content)
        } else {
            alert(caption, content)
        }
    }

    fun winMsgInfo(caption: String?, content: String) {
        if (trayIcon != null && FXApplicationContext.sysVersion!! == "Windows 10") {
            trayIcon!!.showInfoMessage(caption ?: "LabApp™ INFO :", content)
        } else {
            alert(caption, content)
        }
    }

    fun winMsgWarning(caption: String?, content: String) {
        if (trayIcon != null && FXApplicationContext.sysVersion!! == "Windows 10") {
            trayIcon!!.showWarningMessage(caption ?: "LabApp™ Warning :", content)
        } else {
            alert.noticeFailed(content).apply {
                Platform.runLater { show() }
            }
        }
    }

    fun winMsgError(caption: String?, content: String) {
        if (trayIcon != null && FXApplicationContext.sysVersion!! == "Windows 10") {
            trayIcon!!.showErrorMessage(caption ?: "LabApp™ Error :", content)
        } else {
            alert.noticeError(content).apply {
                Platform.runLater { show() }
            }
        }
    }

    private fun alert(caption: String?, content: String) {
        val iv = ImageView(Image(getResourceFile("ticon.png").path))
        iv.fitHeight = 50.0
        iv.fitWidth = 50.0
        alert.notice().apply {
            title(caption)
            text(content)
            graphic(iv)
            hideAfter(Duration.seconds(5.0))
            position(Pos.BOTTOM_RIGHT)
            Platform.runLater { show() }
        }
    }

}